package com.njtoyo.taxi.admin.service.data.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.njtoyo.taxi.admin.library.Const;
import com.njtoyo.taxi.admin.logic.allowance.OrderLogic;
import com.njtoyo.taxi.admin.logic.allowance.RecommendationLogic;
import com.njtoyo.taxi.admin.mapper.master.mapper.*;
import com.njtoyo.taxi.admin.rest.presenter.data.allowance.*;
import com.njtoyo.taxi.admin.rest.wrapper.data.allowance.AllowanceChartWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.data.allowance.AllowanceWrapper;
import com.njtoyo.taxi.admin.service.data.AllowanceCustomerService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service("AllowanceCustomerServiceImpl")
@DS(Const.PRIMARY_DS)
public class AllowanceCustomerServiceImpl implements AllowanceCustomerService {

    @Autowired
    private RideTransactionLogMapper rideTransactionLogMapper;

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private OrderLogic orderLogic;

    @Autowired
    private CustomerIncomeMapper customerIncomeMapper;

    @Autowired
    private RecommendationLogic recommendationLogic;

    @Autowired
    private ActivityWaveAllowanceLogMapper activityWaveAllowanceLogMapper;

    @Autowired
    private RechargeMapper rechargeMapper;

    @Override
    public RestResult<CustomerProportionPresenter> proportion(AdminUser adminUser, AllowanceWrapper wrapper) {
        String startTime = DateUtil.formatDateTime(wrapper.getStartTime());
        String endTime = DateUtil.formatDateTime(wrapper.getEndTime());

        // 优惠券、余额、波浪
        CustomerProportionPresenter res = rideTransactionLogMapper.getTransactionAmount(startTime, endTime);

        // 小程序
        CustomerTotalPresenter hadUsed = customerIncomeMapper.selectTotalAllowance(startTime, endTime);

        // ride_order订单量
        Integer orderNum = orderLogic.getRideOrderCount(startTime, endTime);

        // 乘客推广人数
        Integer recommendationNum = recommendationLogic.getUserCount(startTime, endTime);

        // 乘客推广补贴总金额
        CustomerTotalPresenter recommendationReduction = couponMapper.selectRecommendationAllowanceForReduction(startTime, endTime);
        CustomerTotalPresenter recommendationDiscount = couponMapper.selectRecommendationAllowanceForDiscount(startTime, endTime);
        BigDecimal recommendationValue = NumberUtil.add(recommendationReduction.getTotalValue(), recommendationDiscount.getTotalValue());

        res.setWechatAmount(hadUsed.getTotalValue());
        res.setOrderNum(orderNum);
        res.setRecommendationNum(recommendationNum);
        res.setRecommendationAmount(recommendationValue);

        return RestResult.success(res);
    }

    @Override
    public RestResult<CustomerCouponPresenter> coupon(AdminUser adminUser, AllowanceWrapper wrapper) {
        // 共发放优惠券（金额）-discount_type =》 false\true
        String startTime = DateUtil.formatDateTime(wrapper.getStartTime());
        String endTime = DateUtil.formatDateTime(wrapper.getEndTime());
        CustomerTotalPresenter totalReduction = couponMapper.selectTotalAllowanceForReduction(startTime, endTime);
        CustomerTotalPresenter totalDiscount = couponMapper.selectTotalAllowanceForDiscount(startTime, endTime);

        // 已使用优惠券
        CustomerTotalPresenter hadUsed = couponMapper.selectAllowanceForHadUsed(startTime, endTime);

        CustomerCouponPresenter res = new CustomerCouponPresenter();
        Long totalNum = totalReduction.getTotalNum() + totalDiscount.getTotalNum();
        BigDecimal totalValue = NumberUtil.add(totalReduction.getTotalValue(), totalDiscount.getTotalValue());

        // ride_order单量
        Integer orderNum = orderLogic.getRideOrderCount(startTime, endTime);

        res.setOrderNum(orderNum);
        res.setTotalNum(totalNum);
        res.setTotalValue(totalValue);
        res.setHadUsedNum(hadUsed.getTotalNum());
        res.setHadUsedValue(hadUsed.getTotalValue());
        res.setUserNum(hadUsed.getTotalUser());

        return RestResult.success(res);
    }

    @Override
    public RestResult<CustomerWechatPresenter> wechat(AdminUser adminUser, AllowanceWrapper wrapper) {
        String startTime = DateUtil.formatDateTime(wrapper.getStartTime());
        String endTime = DateUtil.formatDateTime(wrapper.getEndTime());

        CustomerTotalPresenter hadUsed = customerIncomeMapper.selectTotalAllowance(startTime, endTime);

        CustomerWechatPresenter res = new CustomerWechatPresenter();

        res.setHadUsedValue(hadUsed.getTotalValue());
        res.setHadUsedNum(hadUsed.getTotalNum());

        return RestResult.success(res);
    }

    @Override
    public RestResult<CustomerWavePresenter> wave(AdminUser adminUser, AllowanceWrapper wrapper) {
        String startTime = DateUtil.formatDateTime(wrapper.getStartTime());
        String endTime = DateUtil.formatDateTime(wrapper.getEndTime());

        CustomerTotalPresenter total = activityWaveAllowanceLogMapper.selectAllowance(startTime, endTime);

        CustomerWavePresenter res = new CustomerWavePresenter();

        res.setActualAmount(total.getTotalActualAmount());
        res.setNominalAmount(total.getTotalNominalAmount());
        res.setOrderNum(total.getTotalNum());

        return RestResult.success(res);
    }

    @Override
    public RestResult<CustomerBalancePresenter> balance(AdminUser adminUser, AllowanceWrapper wrapper) {
        String startTime = DateUtil.formatDateTime(wrapper.getStartTime());
        String endTime = DateUtil.formatDateTime(wrapper.getEndTime());

        // 发放
        CustomerTotalPresenter totalAdd = rechargeMapper.selectAllowanceForAdd(startTime, endTime);
        // 使用
        CustomerTotalPresenter totalReduct = rechargeMapper.selectAllowanceForReduct(startTime, endTime);

        CustomerBalancePresenter res = new CustomerBalancePresenter();

        res.setTotalValue(totalAdd.getTotalValue());
        res.setHadUsedValue(totalReduct.getTotalValue());
        res.setUserNum(totalReduct.getTotalUser());

        return RestResult.success(res);
    }

    @Override
    public RestResult<List<CustomerCouponChartPresenter>> couponChart(AdminUser adminUser, AllowanceChartWrapper wrapper) {
        return null;
    }

    @Override
    public RestResult<List<CustomerWaveChartPresenter>> waveChart(AdminUser adminUser, AllowanceChartWrapper wrapper) {
        return null;
    }

    @Override
    public RestResult<List<CustomerBalanceChartPresenter>> balanceChart(AdminUser adminUser, AllowanceChartWrapper wrapper) {
        return null;
    }

    @Override
    public RestResult<List<CustomerProportionChartPresenter>> proportionChart(AdminUser adminUser, AllowanceChartWrapper wrapper) {
        return null;
    }
}
